package com.school.revervationSys.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.school.revervationSys.dto.IdDto;
import com.school.revervationSys.dto.ReverVationDto;
import com.school.revervationSys.entity.Revervation;
import com.school.revervationSys.service.IRevervationService;
import com.school.revervationSys.utils.BaseResponse;
import com.school.revervationSys.utils.ErrorCode;
import com.school.revervationSys.utils.ResultUtils;
import com.school.revervationSys.vo.RevervationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author msj
 * @since 2024-04-13
 */
@RestController
@RequestMapping("/revervation")
public class RevervationController  {

    @Autowired
    private IRevervationService revervationService;

    @PostMapping("/addRevervation")
    public BaseResponse addRevervation(@RequestBody Revervation revervation){

        boolean b = revervationService.addRevervation(revervation);
        if(b){
            return ResultUtils.success("新增成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_FAILED,"操作失败");
    }
    @PostMapping("/updateRevervation")
    public BaseResponse updateRevervation(@RequestBody Revervation revervation){

        boolean b = revervationService.updateRevervation(revervation);
        if(b){
            return ResultUtils.success("修改成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_FAILED,"修改失败");
    }

    @GetMapping("/delRevervation")
    public BaseResponse delRevervation(Integer revervationId){

        boolean b = revervationService.delete(revervationId);
        if(b){
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_FAILED,"删除失败");
    }

    @GetMapping("/revationInfo")
    public BaseResponse revationInfo(Integer revervationId){
        RevervationVo revervationVo = revervationService.revationInfo(revervationId);
        return ResultUtils.success(revervationVo);
    }
    @PostMapping("/batchDelRevervation")
    public BaseResponse batchDelete(@RequestBody IdDto param){

        boolean b = revervationService.batchDelete(param.getIds());
        if(b){
            return ResultUtils.success("批量删除成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_FAILED,"批量删除失败");
    }

    @PostMapping("/revervationList")
    public BaseResponse revervationList(@RequestBody ReverVationDto param){
        PageHelper.startPage(param.getPageNo(),param.getPageSize());
        List<Revervation> revervations = revervationService.revervationList(param);
        PageInfo<Revervation> revervationPageInfo = new PageInfo<>(revervations);
        return ResultUtils.success(revervationPageInfo);
    }

}
